package cn.zxcoder.controller.SystemManage.module;

import cn.zxcoder.Tools.ToolUtil;
import cn.zxcoder.entity.AccountRole;
import cn.zxcoder.entity.SystemManage.Menu;
import cn.zxcoder.entity.Res;
import cn.zxcoder.entity.SystemManage.Right;
import cn.zxcoder.entity.SystemManage.Role;
import cn.zxcoder.entity.login.Account;
import cn.zxcoder.service.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.build.Plugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zx
 * @since 2023-05-06
 */
@Api(tags = "获取菜单的一系列数据")
@RestController
@RequestMapping("/api")
public class MenuGetController {

    @Autowired
    IMenuService menuServiceIml;

    @Autowired
    IIconService iIconServiceIml;

    @Autowired
    IRoleService iRoleServiceIml;

    @Autowired
    IRightService iRightServiceIml;

    @Autowired
    IAccountRoleService iAccountRoleServiceIml;

    @ApiOperation("通过id获取菜单")
    @GetMapping("/Module/get/{id}")
    @Transactional(readOnly = true)

    public Res moduleGet(@PathVariable String id) {
        Menu byId = menuServiceIml.getById(Integer.parseInt(id));
        return new Res(byId==null,JSON.toJSONString(byId),byId==null?"查询失败":"查询成功");
    }

    @ApiOperation("获取所有菜单信息")
    @PostMapping("/Module/list")
    @Transactional(readOnly = true)
    public Res moduleList() {

        List<Menu> list = menuServiceIml.list();

        ToolUtil.removeRedundanceMenu(list,menuServiceIml);

        String result = JSON.toJSONString(list);
        return new Res(true,result,"查询成功");

    }

    @ApiOperation("获取所有父级菜单")
    @PostMapping("/Module/nodes")
    @Transactional(readOnly = true)
    public Res moduleNodes(@RequestParam String page,@RequestParam String limit) {
        Page<Menu> menuPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        Page<Menu> page1 = menuServiceIml.page(menuPage);
        String result = JSON.toJSONString(page1);
        return new Res(page1==null,result,page1==null?"查询失败":"查询成功");

    }


    @ApiOperation("获取所有菜单并且展示出来")
    @GetMapping("/menu/{token}")
    @Transactional(readOnly = true)
    public Res getMenu( @PathVariable String token) {
        System.out.println("token:" + token);
        Account account = JSON.parseObject(token, Account.class);
        QueryWrapper<AccountRole> accountRoleQueryWrapper = new QueryWrapper<>();
        accountRoleQueryWrapper.eq("account_id", account.getId());
        Integer roleId = iAccountRoleServiceIml.getOne(accountRoleQueryWrapper).getRoleId();
        //获取此用户的角色id
//        String roleId = account.getRoleId();一对多方式


        if (roleId == null) {
            return new Res(false, null, "您还没有配置权限，请寻找管理员配置权限");
        }

        //找到此角色id对应的所有权限
        Role role = iRoleServiceIml.getById(roleId);
        String roleRights = role.getRoleRights();

        //将所有权限中的菜单权限对应的菜单id放到ids中
        List<Integer> ids = new ArrayList<>();

        String[] _rightIds = roleRights.split(",");
        List<Integer> rightIds = Arrays.stream(_rightIds).map(Integer::parseInt).collect(Collectors.toList());//所有的权限id
        List<Right> rights = iRightServiceIml.listByIds(rightIds);//所有的权限对象集合
        for (Right right : rights) {
            if (!right.isOp()) {
                ids.add(right.getMenuId());
            }
        }

        List<Menu> list = menuServiceIml.listByIds(ids);
//        for (Menu menu : list) {
//            System.out.println(menu.getName());
//        }
        ToolUtil.removeRedundanceMenu(list, menuServiceIml);
        String result = JSON.toJSONString(list);
        return new Res(true, result, "获取菜单信息成功了");
    }



}
